package de.fmannan.wildit.persistence;

import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper  extends SQLiteOpenHelper
{
    private static final String TAG = "DatabaseHelper";

	DatabaseHelper(Context context)
	{
	    super(context, DatabaseAdapter.DATABASE_NAME, null, DatabaseAdapter.DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db)
	{

	    db.execSQL(DatabaseAdapter.getDatabaseCreate());

	    try
	    {
		// Initialise database with default settings
		ContentValues initialValues = new ContentValues();
		initialValues.put(DatabaseAdapter.START_TIME, "08:00:00");
		initialValues.put(DatabaseAdapter.END_TIME, "19:00:00");
		initialValues.put(DatabaseAdapter.INTERVAL, "00:01:00");

		db.insertOrThrow(DatabaseAdapter.TABLE_ALARM, null, initialValues);
	    } catch (SQLException exc)
	    {
		Log.e(TAG,
			"Error while inserting default settings into database: "
				+ exc.getMessage());
	    }
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
	{
	    Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
		    + newVersion + ", which will destroy all old data");
	    db.execSQL("DROP TABLE IF EXISTS " + DatabaseAdapter.TABLE_ALARM);
	    onCreate(db);
	}

}
